package com.soujiayi.activity;

import com.soujiayi.net.EmailSender;
import com.soujiayi.net.NetworkUtils;
import com.soujiayi.util.Constants;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;


public class AdviceAndFeedbackActivity extends Activity {
	
	private EditText et_feedback_content;
	private ScrollView sv_feedback;
	private Handler handler;
	private ProgressDialog progressDialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.advice_and_feedback);
		
		et_feedback_content = (EditText) findViewById(R.id.et_feedback_content);
		sv_feedback = (ScrollView) findViewById(R.id.sv_feedback);
		sv_feedback.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(et_feedback_content.getWindowToken(), 0); 
				return false;
			}
		});
		
		handler = new Handler(){
		    @Override
		    public void handleMessage(Message msg) {
		        super.handleMessage(msg);
		        Bundle data = msg.getData();
		        progressDialog.dismiss();
		        
		        boolean isSuccess = data.getBoolean("isSuccess");

		        if(isSuccess){
					Toast.makeText(
							AdviceAndFeedbackActivity.this,
							AdviceAndFeedbackActivity.this.getResources().getString(
									R.string.advice_and_feedback_success),
							Toast.LENGTH_SHORT).show();
					AdviceAndFeedbackActivity.this.finish();
					overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
		        }else{
					Toast.makeText(
							AdviceAndFeedbackActivity.this,
							AdviceAndFeedbackActivity.this.getResources().getString(
									R.string.send_failure),
							Toast.LENGTH_SHORT).show();
		        }
		    }
		    
		};
	}
	
	public void back(View view) {
		this.finish();
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}
	
	public void submit(View view) {
		if(et_feedback_content.getText() != null && et_feedback_content.getText().toString() != null && !et_feedback_content.getText().toString().trim().equals("")){
			if (NetworkUtils.isNetworkConnected(this)){
				
				progressDialog = new ProgressDialog(AdviceAndFeedbackActivity.this);
				progressDialog.setMessage(getResources().getString(R.string.submit_loading));
				progressDialog.setCancelable(false);
				progressDialog.show();
				
				final String shareMailSubject = AdviceAndFeedbackActivity.this.getResources().getString(R.string.advice_and_feedback_email_subject);
				final String shareContent = et_feedback_content.getText().toString();
	            new Thread(new Runnable() {

	    			@Override
	    			public void run() {
	    				boolean isSuccess = false;
	    				try {
	    					EmailSender sender = new EmailSender();
	    					//设置服务器地址和端口，网上搜的到
	    					sender.setProperties(Constants.EMAILSENDER_SMTP_SERVER, Constants.EMAILSENDER_SMTP_PORT);
	    					//分别设置发件人，邮件标题和文本内容
	    					sender.setMessage(Constants.EMAILSENDER_SENDER, shareMailSubject, shareContent);
	    					//设置收件人
	    					sender.setReceiver(new String[]{Constants.EMAILSENDER_RECEIVER});
	    					//发送邮件
	    					sender.sendEmail(Constants.EMAILSENDER_SMTP_SERVER, Constants.EMAILSENDER_SENDER, Constants.EMAILSENDER_SENDER_PASSWORD);
	    					isSuccess = true;
	    				} catch (Exception e) {
	    					isSuccess = false;
	    					e.printStackTrace();
	    				}
    					Message msg = new Message();
    					Bundle bundle = new Bundle();
    					bundle.putBoolean("isSuccess", isSuccess);
    					msg.setData(bundle);
    					handler.sendMessage(msg);
	    			}
	    		}).start();

			}else{
				Toast.makeText(
						AdviceAndFeedbackActivity.this,
						AdviceAndFeedbackActivity.this.getResources().getString(
								R.string.network_unavailable_message),
						Toast.LENGTH_SHORT).show();
			}
		}else{
			Toast.makeText(
					AdviceAndFeedbackActivity.this,
					AdviceAndFeedbackActivity.this.getResources().getString(
							R.string.advice_and_feedback_empty_hint),
					Toast.LENGTH_SHORT).show();
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//如果按下的是返回键，并且没有重复
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
			return false;
		}
		return false;
	}

}
